package com.nation.controller;

import com.nation.domain.User;
import com.nation.domain.VInternationalAndRelationship;

import com.nation.domain.VPoliticsAndMilitary;
import com.nation.service.RelationshipService;

import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/4.
 */
@Controller
@RequestMapping(value = "/Relationship")
public class RelationshipController {
    @Autowired
    private RelationshipService RelationshipService;

    /**
     * 国际关系
     * @param map
     * @return
     */
    @RequestMapping(value = "/relationship")
    public String development(ModelMap map){
        List<VInternationalAndRelationship> ilist=RelationshipService.findAll();
        map.put("ilist",ilist);
        return "Relationship/relationship";
    }

    /**
     * 修改界面
     * @param country_code
     * @param map
     * @return
     */
    @RequestMapping(value = "/relationship_update")
    public String development_update(String country_code,ModelMap map){
        VInternationalAndRelationship relationship=RelationshipService.findByCountryCode(country_code);
        map.put("relationship",relationship);
        return "Relationship/relationship-update";
    }

    /**
     * 修改信息
     * @param VInternationalAndRelationship
     * @return
     */
    @RequestMapping(value = "/relationship_update.do")
    @ResponseBody
    public String development_pupdate_do(VInternationalAndRelationship VInternationalAndRelationship){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VInternationalAndRelationship.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=RelationshipService.update(VInternationalAndRelationship);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value = "/relationship_add")
    public  String relationship_add(){
        return "Relationship/relationship-add";
    }
    @RequestMapping(value = "/relationship_add.do")
    @ResponseBody
    public String relationship_add_do(HttpServletRequest request, @RequestParam(value = "picture",required = false)MultipartFile[] pictures,
                                      String statistics, String conferences, String diplomatic_cpolicy, String foreign_relation
            , String big_power_relation_asia, String big_power_relation_europe
            , String big_power_relation_africa, String refrence, String country_code
            , String big_power_relation_oceania, String big_power_relation_america
            , String big_power_relation_china, String relation_international_organization
            , String international_agreement){

        VInternationalAndRelationship VInternationalAndRelationship=new VInternationalAndRelationship(statistics, conferences, diplomatic_cpolicy, foreign_relation
                , big_power_relation_asia, big_power_relation_europe
                , big_power_relation_africa, refrence, country_code
                , big_power_relation_oceania, big_power_relation_america
                , big_power_relation_china, relation_international_organization
                , international_agreement);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        VInternationalAndRelationship.setRecoder_id(user.getYHZH());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VInternationalAndRelationship.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=null;
        try{
            if (pictures!=null&&pictures.length>0) {//有文件上传
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.RELATIONSHIP;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.RELATIONSHIP));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                VInternationalAndRelationship.setPicture(picture_path);
            }

            judge=RelationshipService.insert(VInternationalAndRelationship);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败，出错";
        }
        if(judge)
            return "添加成功";
        else
            return "添加失败";
    }
    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request, ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<VInternationalAndRelationship> elist=RelationshipService.findByCondition(map);
        mmap.put("ilist",elist);
        return "Relationship/relationship";
    }
    @RequestMapping(value = "/relationship_delete")
    @ResponseBody
    public String baseinfo_delete(String country_code){
        Boolean judge=null;
        try{
            judge=RelationshipService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
